package com.hp.attendance.dao;

import com.hp.attendance.entity.Employee;
import com.hp.attendance.entity.History;
import com.hp.attendance.util.BaseDAO;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class HistoryDao extends BaseDAO {
    //分页查询
    public List<History> getLimitList(int pageNo, int pageSize) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql ="select * from history limit ?,?";
        Object[] obj ={(pageNo-1)*pageSize,pageSize};
        List<History> employees = queryList(sql, obj, new History());
        return employees;
    }

    //查询总记录条数
    public long getTotal() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql ="select count(1) from history";
        int count = queryCount(sql, new Object[0]);
        return count;
    }

    //根据id查询单个员工信息
    public History getHistory(int id) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql ="select * from history where id=?";
        Object[] obj = {id};
        History history = queryOne(sql, obj, new History());
        return history;

    }

    //修改员工信息,通过id
    public void historyUpdate(History history) throws SQLException, IOException, ClassNotFoundException {
        String sql ="update history set name=?,gender=?,birthday=?,telephone=?,email=?,address=?,education=?,home=?,notes=? where id=?";
        Object[] obj={history.getName(),history.getGender(),history.getBirthday(),history.getTelephone(),history.getEmail(),history.getAddress(),history.getEducation(),history.getHome(),history.getNotes(),history.getId()};
        addOrUpdOrDel(sql,obj);
    }
    //修改员工信息 通过employee_number,没有home字段
    public void historyUpdate1(History history) throws SQLException, IOException, ClassNotFoundException {
        String sql ="update history set name=?,gender=?,birthday=?,telephone=?,email=?,address=?,department_number=?,position_number=?,status=?,education=?,notes=? where employee_number=?";
        Object[] obj={history.getName(),history.getGender(),history.getBirthday(),history.getTelephone(),history.getEmail(),history.getAddress(),history.getDepartment_number(),history.getEmployee_number(), history.getStatus(),history.getEducation(),history.getNotes(),history.getEmployee_number()};
        addOrUpdOrDel(sql,obj);
    }

    public void hisDelete(int employee_number) throws SQLException, IOException, ClassNotFoundException {
        String sql="delete from history where employee_number=?";
        Object[] obj ={employee_number};
        addOrUpdOrDel(sql,obj);
    }

    public void hisAdd(History history) throws SQLException, IOException, ClassNotFoundException {
        String sql="insert into history values(null,?,?,?,?,?,?,?,null,?,?,null,?,?,?,null,?)";
        Object[] obj={history.getEmployee_number(),history.getName(),history.getGender(),
                history.getBirthday(),history.getTelephone(),history.getEmail(),history.getAddress(),
                history.getEducation(),history.getIn_time(),history.getDepartment_number(), history.getPosition_number(),history.getStatus(),history.getNotes()};
        addOrUpdOrDel(sql,obj);
    }

    //查询所有his表信息
    public List<History> getList() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql ="select * from history";
        List<History> list = queryList(sql, new Object[0], new History());
        return list;
    }
}
